/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on 2011-7-23, 16:22:38
 */
package edu.qmul.ns2;

/**
 *
 * @author Moon
 */
public class MainPanel extends javax.swing.JPanel {

    private DrawElement element;

    /** Creates new form MainPanel */
    public MainPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });
        addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentRemoved(java.awt.event.ContainerEvent evt) {
                formComponentRemoved(evt);
            }
        });
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        this.requestFocus();
    }//GEN-LAST:event_formMousePressed

    private void formComponentRemoved(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_formComponentRemoved
        evt.getChild().setVisible(false);
    }//GEN-LAST:event_formComponentRemoved

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    public void setDrawElement(DrawElement element) {
        DrawElement old = this.element;
        if (element == this.element) {
            return;
        }
        synchronized (getTreeLock()) {
            this.element = element;
            if (old != null) {
                this.removeMouseListener(old);
                this.removeMouseMotionListener(old);
                old.stop(this);
            }
            if (element != null) {
                element.start(this);
                this.addMouseListener(element);
                this.addMouseMotionListener(element);
            }
        }
    }
}
